package com.tools.module.base.service.impl;

import com.tools.common.dynamicquery.DynamicQuery;
import com.tools.common.model.PageBean;
import com.tools.common.model.Result;
import com.tools.common.util.DateUtils;
import com.tools.module.base.entity.BaseGroup;
import com.tools.module.base.repository.BaseConsultRepository;
import com.tools.module.base.entity.BaseConsult;
import com.tools.module.base.service.BaseConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BaseConsultServiceImpl implements BaseConsultService {

    @Autowired
    private DynamicQuery dynamicQuery;
    @Autowired
    private BaseConsultRepository consultRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result save(BaseConsult consult) {
        consult.setCreateTime(DateUtils.getTimestamp());
        consult.setUpdateTime(DateUtils.getTimestamp());
        consultRepository.saveAndFlush(consult);
        return Result.ok("保存成功");
    }

    @Override
    public Result get(Long id) {
        BaseConsult consult = consultRepository.getOne(id);
        return Result.ok(consult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result delete(Long id) {
        consultRepository.deleteById(id);
        return Result.ok("删除成功");
    }

    @Override
    public Result list(BaseConsult consult) {
//        String nativeSql = "SELECT COUNT(*) FROM base_consult";
//        Long count = dynamicQuery.nativeQueryCount(nativeSql);
//        PageBean<BaseConsult> data = new PageBean<>();
//        if(count>0){
//            nativeSql = "SELECT * FROM base_consult ORDER BY gmt_create desc";
//            Pageable pageable = PageRequest.of(consult.getPageNo(),consult.getPageSize());
//            List<BaseConsult> list =  dynamicQuery.nativeQueryPagingList(BaseConsult.class,pageable,nativeSql);
//            data = new PageBean(list,count);
//        }
//        return Result.ok(data);

        String nativeSql = "SELECT COUNT(*) FROM base_consult";
        if (consult.getType() != null) {
            nativeSql = "SELECT COUNT(*) FROM base_consult  where  type =" + consult.getType() + "order by seq";
        }

        Long count = dynamicQuery.nativeQueryCount(nativeSql);
        PageBean<BaseGroup> data = new PageBean<>();
        if (count > 0) {
            nativeSql = "SELECT * FROM base_consult  order type,seq";
            if (consult.getType() != null) {
                nativeSql = "SELECT * FROM  base_consult  where  type =" + consult.getType() + "order by seq";
            }
            Pageable pageable = PageRequest.of(consult.getPageNo(), consult.getPageSize());
            List<BaseGroup> list = dynamicQuery.nativeQueryPagingList(BaseGroup.class, pageable, nativeSql);
            data = new PageBean(list, count);
        }
        return Result.ok(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateSeq(BaseConsult consult) {
        String nativeSql = "update base_consult set seq = seq +1 where seq >= ? and type = ?";
        dynamicQuery.nativeExecuteUpdate(nativeSql,consult.getSeq(),consult.getType());
        return Result.ok("修改成功");
    }

    @Override
    public Long queryBySeq(BaseConsult consult) {
        String nativeSql = "SELECT COUNT(*) FROM base_consult where  type =" + "\"" + consult.getType() + "\""+ "and seq = "+consult.getSeq();
        Long count = dynamicQuery.nativeQueryCount(nativeSql);

        return count;
    }
}
